package server;

import java.util.LinkedList;

/**
 * Class FarmerHandler
 * 		The purpose of this class is to handle any changes to the
 * farmer database. This includes changing account information or
 * resetting lockouts on accounts.
 * @author vegliantin, pknintendodude
 *
 */
public class FarmerHandler {
    
    public static LinkedList<Farmer> farmList = new LinkedList<Farmer>();
    private static int count=0;
    
    public FarmerHandler(){
        if(farmList.isEmpty()){
            TextInterface.loadFarmers();
            makeAdm("admin", "admin@gmail.com", "test".hashCode(),5, " ", " ");
            if(farmList == null){
            	farmList = new LinkedList<Farmer>();
            	TextInterface.saveFarmers();
            }
        }
    }
    
	/**
	 * Method makeFarmer
	 *  Makes a new farmer
	 * @param name The user's real name
	 * @param email The username and user's email address
	 * @param passHash The hashed password (prevents using password directly)
	 * @param pubLocation The user's hardiness zone
	 * @param privLocation The user's actual living address
	 * @param aboutMe Any information that the user wants to provide
	 * @return
	 */
    
    
	public boolean makeFarmer(String name, String email, int passHash, int pubLocation,
			String privLocation, String aboutMe)
	{
		boolean register = false;
		try
		{
			Farmer user = new Farmer( name, email, passHash, pubLocation, privLocation, aboutMe);
			farmList.add(user);
                        register = true;
                        count++;
            TextInterface.saveFarmers();
		}
		
		catch (Exception e)
		{
			register = false;
		}
		
		finally 
		{
		return register;
		}
	}
	
	/**
	 * Method makeAdm
	 * 	This method creates a user with admin privileges. Otherwise same as makeFarmer.
	 * (Supposed to be a testing class, not meant to be implemented in actual application)
	 * @param name User's real name
	 * @param email User's email address and username
	 * @param passHash The user's hashed password
	 * @param pubLocation The user's hardiness zone
	 * @param privLocation The user's actual living address
	 * @param aboutMe Personal informtaion the user wants to provide
	 * @return true on successful user creation, false otherwise
	 */
	public boolean makeAdm(String name, String email, int passHash, int pubLocation,
			String privLocation, String aboutMe)
	{
		boolean register = false;
		try
		{
			Farmer user = new Farmer( name, email, passHash, pubLocation, privLocation, aboutMe);
			user.setIsAdmin(true);
			farmList.add(user);
                        register = true;
                        count++;
            TextInterface.saveFarmers();
		}
		
		catch (Exception e)
		{
			register = false;
		}
		
		finally 
		{
		return register;
		}
	}
        
	/**
	 * Method getFarmer
	 * 	Gets a farmer from the persistent data storage.
	 * @param email The username
	 * @return null if data store is empty, the user otherwise
	 */
	public Farmer getFarmer(String email){
            TextInterface.loadFarmers();
            if(!farmList.isEmpty()){
                for(Farmer f : farmList){
                    if(f.getEmail().equals(email)){
                        return f;
                    }
                }
            }
            return null;
        }
        
	
		/**
		 * Method changePassword
		 * 	Changes the person's password to the password given
		 * @param email username
		 * @param password the new password for given username
		 * @return true on successful change, false otherwise
		 */
        public boolean changePassword(String email, String password){
            return false;
        }
        
        public boolean updateFarmer(String email, String paramName, String changeTo){
            return false;
        }
        
        public boolean removeFarmer(String email){
            return false;
        }
        
        /**
         * Method toggleLockout
         * 	
         * @param bool The new condition for the 
         * @param email The account getting changed
         */
        public void toggleLockout(Boolean bool, String email){
            Farmer f=getFarmer(email);
            f.setLockout(bool);
        }
        
        
	
}
